package com.q.tqb.activity.mine;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.q.tqb.R;
import com.q.tqb.adapter.BaseFragmentPagerAdapter;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.fragment.MineGrabRedFragment;
import com.q.tqb.fragment.MineSendRedFragment;
import com.q.tqb.util.DensityUtil;
import com.q.tqb.util.SPUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class MineRedEnvelopeActivity extends BaseActivity {

    @BindView(R.id.tab_red_envelope)
    TabLayout mTabRedEnvelope;
    @BindView(R.id.vp_red_envelope)
    ViewPager mVpRedEnvelope;
    private BaseFragmentPagerAdapter mFragmentPagerAdapter;
    private ViewHolder holder;
    private int uid;


    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_red_envelope;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        List<Fragment> fragments = new ArrayList<>();
        uid = getIntent().getIntExtra("uid", 0);
        if (uid == 0) {
            uid = (int) SPUtil.get(this, "uid", 0);
            setTitle("我的红包");
        } else {
            setTitle("他的红包");
        }


        MineSendRedFragment mineSendRedFragment = MineSendRedFragment.newInstance(uid);
        MineGrabRedFragment mineGrabRedFragment = MineGrabRedFragment.newInstance(uid);
        fragments.add(mineSendRedFragment);
        fragments.add(mineGrabRedFragment);

        String[] tabStrings = getResources().getStringArray(R.array.MineRedEnvelope);
        mFragmentPagerAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), this, fragments, tabStrings);
        mTabRedEnvelope.setTabMode(TabLayout.MODE_FIXED);

        LinearLayout linearLayout = (LinearLayout) mTabRedEnvelope.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.divider));
        linearLayout.setDividerPadding(DensityUtil.dip2px(MineRedEnvelopeActivity.this, 10.0f)); //设置分割线间隔

        mVpRedEnvelope.setAdapter(mFragmentPagerAdapter);
        mTabRedEnvelope.setupWithViewPager(mVpRedEnvelope);

        setTabView();

        mTabRedEnvelope.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabRedEnvelope, 50, 50);
            }
        });
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        assert llTab != null;
        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }

    }

    private void setTabView() {
        String[] tabStrings = getResources().getStringArray(R.array.MineRedEnvelope);
        String[] tabNumbers = new String[2];
        String getMoney = SPUtil.getString(this, "getMoney");
        String postMoney = SPUtil.getString(this, "postMoney");
        tabNumbers[0] = postMoney + "元";
        tabNumbers[1] = getMoney + "元";
        holder = null;
        for (int i = 0; i < tabStrings.length; i++) {
            //依次获取标签
            TabLayout.Tab tab = mTabRedEnvelope.getTabAt(i);
            //为每个标签设置布局
            tab.setCustomView(R.layout.tab_item);
            holder = new ViewHolder(tab.getCustomView());
            //为标签填充数据
            holder.tvTabName.setText(tabStrings[i]);
            holder.tvTabNumber.setText(String.valueOf(tabNumbers[i]));
            //默认选择第一项
            if (i == 0) {
                holder.tvTabName.setSelected(true);
                holder.tvTabNumber.setSelected(true);
                holder.tvTabName.setTextSize(16);
                holder.tvTabNumber.setTextSize(16);
            }
        }

        //tab选中的监听事件
        mTabRedEnvelope.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                holder = new ViewHolder(tab.getCustomView());
                holder.tvTabName.setSelected(true);
                holder.tvTabNumber.setSelected(true);
                //选中后字体变大
                holder.tvTabName.setTextSize(16);
                holder.tvTabNumber.setTextSize(16);
                //让Viewpager跟随TabLayout的标签切换
                mVpRedEnvelope.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                holder = new ViewHolder(tab.getCustomView());
                holder.tvTabName.setSelected(false);
                holder.tvTabNumber.setSelected(false);
                //恢复为默认字体大小
                holder.tvTabName.setTextSize(14);
                holder.tvTabNumber.setTextSize(14);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    class ViewHolder {
        TextView tvTabName;
        TextView tvTabNumber;

        public ViewHolder(View tabView) {
            tvTabName = (TextView) tabView.findViewById(R.id.tv_tab_name);
            tvTabNumber = (TextView) tabView.findViewById(R.id.tv_tab_number);
        }
    }
}
